<?php
/**
 *Create by PhpStorm
 *User:Kendy
 *Date:2019/10/8/009
 *Time:13:50
 */

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response;
use app\common\model\GuideOrderModel as gom;
use app\common\model\HotelOrderModel as hom;
use app\common\model\TourOrderModel as tom;
use app\common\model\GtourOrderModel as gtom;
use app\admin\model\OrderComplains as ocs;
use app\admin\model\OrderRefunds as ors;
use app\admin\model\Orders;
class Order extends Common
{
    /**
     *  导游订单列表
     */
    public function guidelist()
    {
        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);

            $where['is_del'] = array('eq', 0);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['name']) && $data['name'] != '') {
                $where['rename'] = array('like', '%' . $data['name'] . '%');
            }
            if (isset($data['order_status']) && $data['order_status'] != '') {
                $where['order_status'] = $data['order_status'];
            }
            $guide = new gom();
            $rdata = $guide->getGuideOrderList($where, $this->from,$this->size);
            $count = $guide->getGuideOrderCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
            return view();
        }
    }


    /**
     * 获取导游订单的详细信息
     */
    public function  guideOrderDetail()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}";
            $guide = new gom();
            $guideOrderInfo = $guide->getGuideOrderById($where);
            if ($guideOrderInfo) {
                ajax_return(config('code.success_show'), '请求成功', $guideOrderInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }
    public function  delGuide()
    {   
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id = {$postData['id']}";
            $guide = new gom();
            $res = $guide->guideDel($where);
            if ($res) {
                ajax_return(config('code.success_show'), '删除成功', 0);
            } else {
                ajax_return(config('code.error_show'), '删除失败', $res);
            }
        }
    }

    /**
     *  酒店房间订单列表
     */
    public function  hotellist()
    {
        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page); 
            $where['ho.is_del'] = array('eq', 0);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['hotel_name']) && $data['hotel_name'] != '') {
                $where['h.hotel_name'] = array('like', '%' . $data['hotel_name'] . '%');
            }
            if (isset($data['order_status']) && $data['order_status'] != '') {
                $where['order_status'] = $data['order_status'];
            }
           if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['ho.create_time']=['between',$created_at]; 
            } 
            $hom = new hom();
            $rdata = $hom->getHotelOrderList($where, $this->from,$this->size);
            $count = $hom->getHotelOrderCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
            $res = Db::name('hotel_order')->field('order_sn')->select();
            $this->assign('res', $res);
            $hotel_name = Db::name('hotel')->field("hotel_name")->where(['is_display'=>0,'is_del'=>0,'is_pass'=>1])->select();
            $this->assign('hotel_name', $hotel_name);
            return view();
        }
    }

    /**
     * 获取导游订单的详细信息
     */
    public function  hotelOrderDetail()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}";
            $guide = new hom();
            $guideOrderInfo = $guide->getHotelOrderById($where);
            ajax_return(config('code.success_show'), '请求成功', $guideOrderInfo);
        }
    }

    /**
     *  景点订单列表
     */
    public function  tourlist()
    {
        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);

            $where['to.is_del'] = array('eq', 0);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['tour_name']) && $data['tour_name'] != '') {
                $where['t.tour_name'] = array('like', '%' . $data['tour_name'] . '%');
            }
            if (isset($data['order_status']) && $data['order_status'] != '') {
                $where['order_status'] = $data['order_status'];
            }
           if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['to.create_time']=['between',$created_at]; 
            } 
            $guide = new tom();
            $rdata = $guide->getTourOrderList($where, $this->from,$this->size);
            $count = $guide->getTourOrderCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
            $res = Db::name('tour_order')->field('order_sn')->select();
            $this->assign('res', $res);
            $tour_name = Db::name('tours')->field("tour_name")->where(['is_display'=>0,'is_del'=>0,'is_pass'=>1])->select(); 
            $this->assign('tour_name', $tour_name);
            return view();
        }
    }
    /**
     * 获取景点订单的详细信息
     */
    public function  tourOrderDetail()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}";
            $guide = new tom();
            $guideOrderInfo = $guide->getTourOrderById($where);
            if ($guideOrderInfo) {
                ajax_return(config('code.success_show'), '请求成功', $guideOrderInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }

    /**
     * 跟团订单列表
     */
    public function  gtourlist()
    {
        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);

            $where['go.is_del'] = array('eq', 0);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['gtour_title']) && $data['gtour_title'] != '') {
                $where['gt.gtour_title'] = array('like', '%' . $data['gtour_title'] . '%');
            }
            if (isset($data['order_status']) && $data['order_status'] != '') {
                $where['order_status'] = $data['order_status'];
            }
            if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['go.create_time']=['between',$created_at]; 
            } 
            $gtour = new gtom();
            $rdata = $gtour->getGtourOrderList($where, $this->from,$this->size);
            $count = $gtour->getGtourOrderCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
          $res = Db::name('gtour_order')->field('order_sn')->select();
          $this->assign('res', $res);
          $gtour_title = Db::name('group_tour')->field("gtour_title")->where(['is_display'=>0,'is_del'=>0])->select(); 
          $this->assign('gtour_title', $gtour_title);
            return view();
        }
    }
    /**
     * 获取跟团订单的详细信息
     */
    public function  gtourOrderDetail()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}";
            $gtour = new gtom();
            $guideOrderInfo = $gtour->getGtourOrderById($where);
            if ($guideOrderInfo) {
                ajax_return(config('code.success_show'), '请求成功', $guideOrderInfo);
            } else {
                ajax_return(config('code.error_show'), '请求失败');
            }
        }
    }
    /**
     * 投诉订单管理
     */
    public function  complainlist()
    {
        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['complain_status']) && $data['complain_status'] != '') {
                $where['complain_status'] = $data['complain_status'];
            }
            if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['oc.create_time']=['between',$created_at]; 
            } 
            $complain = new ocs();
            $where['u.is_del'] = 0;
            $rdata = $complain->getComplainList($where, $this->from,$this->size);
            if(count($rdata)>0){
                foreach ($rdata as $key => $value) {
                     $rdata[$key]['create_time'] = date("Y-m-d H:i:s",$value['create_time']);
                }
            }
            $count = $complain->getComplainCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
            $complain = new ocs();
            $res = Db::name("order_complains")->field('order_sn')->select(); 
            $this->assign('res', $res);
            return view();
        }
    }

    /**
     * 退款订单管理
     */
    public function  refundlist()
    {

        if (request()->isAjax()) {
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            if (isset($data['order_sn']) && $data['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $data['order_sn'] . '%');
            }
            if (isset($data['refund_status']) && $data['refund_status'] != '') {
                $where['refund_status'] = $data['refund_status'];
            }
            if (isset($data['create_time']) && $data['create_time'] != ''){
                $created_at=explode(' - ', $data['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['or.create_time']=['between',$created_at]; 
            } 
            $complain = new ors();
            $where['u.is_del'] = 0;
            $rdata = $complain->getRefundList($where, $this->from,$this->size);
             if(count($rdata)>0){
                foreach ($rdata as $key => $value) {
                    if($value['refund_time']){ 
                       $rdata[$key]['refund_time'] = date("Y-m-d H:i:s",$value['refund_time']);
                    }else{
                         $rdata[$key]['refund_time'] ="未设置";
                    }
                     $rdata[$key]['create_time'] = date("Y-m-d H:i:s",$value['create_time']);
                     //查询投诉的商家名称
                      //2景点3跟团4酒店  
                      if($value['object_type']==2){
                         $rdata[$key]['object_name'] = Db::name('tours')->where(['tour_id'=>$value['object_id']])->value("tour_name");
                      }
                      if($value['object_type']==3){  
                         $rdata[$key]['object_name'] = Db::name('group_tour')
                         ->alias('gt')
                         ->join("team t",'t.team_id=gt.team_id','left')
                         ->where(['id'=>$value['object_id']])
                         ->value("team_name");

                      }
                      if($value['object_type']==4){  
                         $rdata[$key]['object_name'] = Db::name('hotel') 
                         ->where(['hotel_id'=>$value['object_id']])
                         ->value("hotel_name");

                      }

                }
            }
            $count = $complain->getRefundListCount($where);
            ajax_return(config('code.success_show'), '请求成功', $rdata, $count);
        } else {
           $res = Db::name("order_refund")->field('order_sn')->select(); 
            $this->assign('res', $res);
            return view();
        }
    }
    /**
     * [refundOrderDetail 退款详情]
     * @Author   kendy
     * @DateTime 2020-01-08T08:54:52+0800
     * @return   [type]                   [description]
     */
    public function refundOrderDetail()
    { 
       if (request()->isAjax()) {
            $postData = input('param.'); 
            $order_sn = $postData['order_sn']; 
            $object_type = $postData['object_type'];
            $object_id = $postData['object_id'];
            $order = new orders();
            $refundOrderDetail = $order->getOrderInfo($object_id,$order_sn,$object_type); 
            ajax_return(config('code.success_show'), '请求成功', $refundOrderDetail); 
        }
    }
}